home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / thread_wince.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  4.0 KB  |  171 lines

  1. /* This code implemented by Mark Hammond (MHammond@skippinet.com.au) */
  2.  
  3. #include <windows.h>
  4. #include <limits.h>
  5. #include <pydebug.h>
  6.  
  7. long PyThread_get_thread_ident(void);
  8.  
  9. /*
  10.  * Change all headers to pure ANSI as no one will use K&R style on an
  11.  * NT
  12.  */
  13.  
  14. /*
  15.  * Initialization of the C package, should not be needed.
  16.  */
  17. static void PyThread__init_thread(void)
  18. {
  19. }
  20.  
  21. /*
  22.  * Thread support.
  23.  */
  24. int PyThread_start_new_thread(void (*func)(void *), void *arg)
  25. {
  26.     long rv;
  27.     int success = 0;
  28.  
  29.     dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
  30.     if (!initialized)
  31.         PyThread_init_thread();
  32.  
  33.     rv = _beginthread(func, 0, arg); /* use default stack size */
  34.  
  35.     if (rv != -1) {
  36.         success = 1;
  37.         dprintf(("%ld: PyThread_start_new_thread succeeded:\n", PyThread_get_thread_ident()));
  38.     }
  39.  
  40.     return success;
  41. }
  42.  
  43. /*
  44.  * Return the thread Id instead of an handle. The Id is said to uniquely identify the
  45.  * thread in the system
  46.  */
  47. long PyThread_get_thread_ident(void)
  48. {
  49.     if (!initialized)
  50.         PyThread_init_thread();
  51.         
  52.     return GetCurrentThreadId();
  53. }
  54.  
  55. static void do_PyThread_exit_thread(int no_cleanup)
  56. {
  57.     dprintf(("%ld: do_PyThread_exit_thread called\n", PyThread_get_thread_ident()));
  58.     if (!initialized)
  59.         if (no_cleanup)
  60.             exit(0); /* XXX - was _exit()!! */
  61.         else
  62.             exit(0);
  63.     _endthread();
  64. }
  65.  
  66. void PyThread_exit_thread(void)
  67. {
  68.     do_PyThread_exit_thread(0);
  69. }
  70.  
  71. void PyThread__exit_thread(void)
  72. {
  73.     do_PyThread_exit_thread(1);
  74. }
  75.  
  76. #ifndef NO_EXIT_PROG
  77. static void do_PyThread_exit_prog(int status, int no_cleanup)
  78. {
  79.     dprintf(("PyThread_exit_prog(%d) called\n", status));
  80.     if (!initialized)
  81.         if (no_cleanup)
  82.             _exit(status);
  83.         else
  84.             exit(status);
  85. }
  86.  
  87. void PyThread_exit_prog(int status)
  88. {
  89.     do_PyThread_exit_prog(status, 0);
  90. }
  91.  
  92. void PyThread__exit_prog _P1(int status)
  93. {
  94.     do_PyThread_exit_prog(status, 1);
  95. }
  96. #endif /* NO_EXIT_PROG */
  97.  
  98. /*
  99.  * Lock support. It has to be implemented using Mutexes, as
  100.  * CE doesnt support semaphores.  Therefore we use some hacks to
  101.  * simulate the non reentrant requirements of Python locks
  102.  */
  103. PyThread_type_lock PyThread_allocate_lock(void)
  104. {
  105.     HANDLE aLock;
  106.  
  107.     dprintf(("PyThread_allocate_lock called\n"));
  108.     if (!initialized)
  109.         PyThread_init_thread();
  110.  
  111.     aLock = CreateEvent(NULL,           /* Security attributes      */
  112.                         0,              /* Manual-Reset               */
  113.                         1,              /* Is initially signalled  */
  114.                         NULL);          /* Name of event            */
  115.  
  116.     dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock));
  117.  
  118.     return (PyThread_type_lock) aLock;
  119. }
  120.  
  121. void PyThread_free_lock(PyThread_type_lock aLock)
  122. {
  123.     dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  124.  
  125.     CloseHandle(aLock);
  126. }
  127.  
  128. /*
  129.  * Return 1 on success if the lock was acquired
  130.  *
  131.  * and 0 if the lock was not acquired. This means a 0 is returned
  132.  * if the lock has already been acquired by this thread!
  133.  */
  134. int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
  135. {
  136.     int success = 1;
  137.     DWORD waitResult;
  138.  
  139.     dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),(long)aLock, waitflag));
  140.  
  141. #ifndef DEBUG
  142.     waitResult = WaitForSingleObject(aLock, (waitflag == 1 ? INFINITE : 0));
  143. #else
  144.     /* To aid in debugging, we regularly wake up.  This allows us to
  145.     break into the debugger */
  146.     while (TRUE) {
  147.         waitResult = WaitForSingleObject(aLock, waitflag ? 3000 : 0);
  148.         if (waitflag==0 || (waitflag==1 && waitResult == WAIT_OBJECT_0))
  149.             break;
  150.     }
  151. #endif
  152.  
  153.     if (waitResult != WAIT_OBJECT_0) {
  154.         success = 0;    /* We failed */
  155.     }
  156.  
  157.     dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n", PyThread_get_thread_ident(),(long)aLock, waitflag, success));
  158.  
  159.     return success;
  160. }
  161.  
  162. void PyThread_release_lock(PyThread_type_lock aLock)
  163. {
  164.     dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  165.  
  166.     if (!SetEvent(aLock))
  167.         dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n", PyThread_get_thread_ident(), (long)aLock, GetLastError()));
  168. }
  169.  
  170.  
  171.